package com.example.design.filter;


import com.example.design.filter.context.PreSaleScheduleContext;
import com.example.design.filter.context.ScheduleUser;
import com.example.design.filter.filter.*;

import java.util.ArrayList;
import java.util.List;

public class PreSaleFilterChain {

    private static final List<ScheduleShiftFilter> day_work_filter_chain = new ArrayList<>(4);

    private static final List<ScheduleShiftFilter> night_work_filter_chain = new ArrayList<>(3);


    static {
        day_work_filter_chain.add(ShiftNumFilter.instance);
        day_work_filter_chain.add(DayShiftNumFilter.instance);
        day_work_filter_chain.add(NoDayTagFilter.instance);
        day_work_filter_chain.add(NoDayAfterNightFilter.instance);

        night_work_filter_chain.add(ShiftNumFilter.instance);
        night_work_filter_chain.add(NoNightTagFilter.instance);
    }

    public static void doFilter(PreSaleScheduleContext context) {
        // 根据uid排序 单月正序/双月倒序
        context.sort();
        // 优先排班
        List<ScheduleUser> users = new ArrayList<>(context.getPriorityUsers());
        users.addAll(context.getScheduleUsers());

        for (ScheduleUser user : users) {
            if (doDayWorkFilter(context, user)) {
                continue;
            }
            if (doNightWorkFilter(context, user)) {
                continue;
            }
            context.addRestUser(user);
        }
    }

    private static boolean doDayWorkFilter(PreSaleScheduleContext context, ScheduleUser user) {
        for (ScheduleShiftFilter filter : day_work_filter_chain) {
            if (!filter.filter(context, user)) {
                return false;
            }
        }
        context.addDayWorkUser(user);
        return true;
    }

    private static boolean doNightWorkFilter(PreSaleScheduleContext context, ScheduleUser user) {
        for (ScheduleShiftFilter filter : night_work_filter_chain) {
            if (!filter.filter(context, user)) {
                return false;
            }
        }
        context.addNightWorkUser(user);
        return true;
    }

}
